/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.menu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import org.jxSwing.i18n.I18N;
import org.jxSwing.laf.LookAndFeelUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.menu
 * @date 17/11/2014 - 18:23:26
 */
public class LookAndFeelMenu extends JMenu {
	
	private static final long serialVersionUID = -6759670045944651501L;
	
	private static final List<JCheckBoxMenuItem> MENU_ITEMS = new ArrayList<>();
	
	private Component component;
	
	/**
	 * @param frame
	 */
	public LookAndFeelMenu(Component frame) {
	
		this.component = frame;
		
		initGUI();
		
		// 
		new Thread(new Runnable() {
			
			@Override
			public void run() {
			
				try {
					Thread.sleep(1000);
				}catch(InterruptedException interruptedException) {
					// ignore
				}
				
				// 
				for(JCheckBoxMenuItem item : MENU_ITEMS) {
					
					item.setSelected(LookAndFeelUtil.getLookAndFeel().getName().equals(item.getText()));
				}
			}
		}).start();
	}
	
	private void initGUI() {
	
		setText(I18N.component().theme());
		
		//
		for(final LookAndFeelInfo laf : LookAndFeelUtil.getInstalledLookAndFeelsClass()) {
			
			JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(laf.getName());
			
			menuItem.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent event) {
				
					try {
						//
						if(getComponent() != null) {
							
							LookAndFeelUtil.applyLookAndFeel(laf.getClassName(), getComponent());
							
						}else {
							
							LookAndFeelUtil.applyLookAndFeel(laf.getClassName());
						}
						
						// 
						for(JCheckBoxMenuItem item : MENU_ITEMS) {
							
							item.setSelected(LookAndFeelUtil.getLookAndFeel().getName().equals(item.getText()));
						}
						
					}catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exception) {
						
						exception.printStackTrace();
					}
				}
			});
			
			MENU_ITEMS.add(menuItem);
			
			add(menuItem);
		}
	}
	
	/**
	 * @return the component
	 */
	public final Component getComponent() {
	
		return component;
	}
}
